package anshe.net
{
	import anshe.event.AnsheNetEvent;
	
	import flash.events.AsyncErrorEvent;
	import flash.events.IOErrorEvent;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.NetConnection;

	public class RedConnection extends NetConnection
	{	
		private static var m_nc:RedConnection;		//	内置对象
		public static function getNC():RedConnection
		{
			if(!m_nc)
			{
				m_nc = new RedConnection();
			}
			return m_nc;
		}
		
		public function RedConnection()
		{
			this.addEventListener(NetStatusEvent.NET_STATUS, onNetStatueEvent);
			this.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onFindErrorEvent);
			this.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onFindErrorEvent);
			this.addEventListener(IOErrorEvent.IO_ERROR, onFindErrorEvent);
		}
		
		//	NetConnection 对象返回的事件（含连接成功，服务器断开及连接异常）
		private function onNetStatueEvent(e:NetStatusEvent):void
		{	
			trace(e.info.code,e.info.level);
			
			if(e.info.code == "NetConnection.Connect.Success")
			{
				dispatchEvent(new AnsheNetEvent(AnsheNetEvent.SERVER_CONN_COMPLETE));
			}
			else if(e.info.code == "NetConnection.Connect.Closed")
			{
				dispatchEvent(new AnsheNetEvent(AnsheNetEvent.SERVER_CLOSE));
			}		
			else
			{
				dispatchEvent(new AnsheNetEvent(AnsheNetEvent.SERVER_CONN_ERROR, e));
			}
		}
		
		//	NetConnection 连接出错（通常指 IO 错误及踌域访问出错）
		public function onFindErrorEvent(e:*):void
		{
			dispatchEvent(new AnsheNetEvent(AnsheNetEvent.SERVER_CONN_ERROR, e));
		}
		
		public function setClientInfo():void
		{
		    dispatchEvent(new AnsheNetEvent(AnsheNetEvent.GET_USER_INFO, arguments));
		}
		
		public function setServerInfo():void
		{
		    dispatchEvent(new AnsheNetEvent(AnsheNetEvent.GET_SERVER_INFO, arguments));
		}		
		
		public function serverSystemBroadcast():void	
		{
			dispatchEvent(new AnsheNetEvent(AnsheNetEvent.SERVER_BROADCAST, arguments));
		}
		
		public function serverReturn():void
		{
		    dispatchEvent(new AnsheNetEvent(AnsheNetEvent.SERVER_MESSAGE, arguments));
		}
	}
}